package com.sita.tboard.advertisement;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sita.bike.R;
import com.sita.bike.support.Constants;
import com.sita.bike.support.GlobalContext;
import com.sita.friend.ui.activity.MainActivity;
import com.squareup.picasso.Picasso;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by xiaodong on 16/3/5.
 */
public class AdShowActivity extends AppCompatActivity {
    private CountDownTimer countDownTimer;

    @Bind(R.id.timer)
    TextView timerView;

    @Bind(R.id.big_pic)
    ImageView bigpicView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_adshow);
        ButterKnife.bind(this);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        AdModel adModel = AdvertiseManager.getInstance().getAdModel();
        if (adModel != null){
            Picasso.with(GlobalContext.getGlobalContext()).load(adModel.bitmap).fit().centerInside().into(bigpicView);
            bigpicView.setClickable(true);
        } else {
            bigpicView.setClickable(false);
        }
        countDownTimer = new CountDownTimer(Constants.ADVERTISEMENT_SHOW_TIME,
                Constants.ADVERTISEMENT_INTERVAL) {
            @Override
            public void onTick(long l) {
                String time = String.valueOf((int) Math.round(l / 1000.0) - 1);
                timerView.setText(time);
            }

            @Override
            public void onFinish() {
                Intent intent = new Intent(AdShowActivity.this, MainActivity.class);
                AdShowActivity.this.startActivity(intent);
                AdShowActivity.this.finish();
            }
        };
        countDownTimer.start();
        bigpicView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AdShowActivity.this, AdDetailActivity.class);
                AdShowActivity.this.startActivity(intent);
                if (countDownTimer != null) {
                    countDownTimer.cancel();
                    countDownTimer = null;
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null){
            countDownTimer.cancel();
        }
    }

    @Override
    public void onBackPressed() {
        // nothing
    }


}
